package com.keeson.android_cli.presenter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import com.keeson.android_cli.R;
import com.keeson.android_cli.api.Api;
import com.keeson.android_cli.base.BasePresenterImpl;
import com.keeson.android_cli.base.BaseRespose;
import com.keeson.android_cli.base.retrofit.BaseObserver;
import com.keeson.android_cli.base.retrofit.RxSchedulers;
import com.keeson.android_cli.bean.request.LoginRequest;
import com.keeson.android_cli.entity.UserInfo;
import com.keeson.android_cli.interfaces.contract.LoginContract;
import com.keeson.android_cli.util.CommonUtils;
import com.keeson.android_cli.util.CustomToast;
import com.keeson.android_cli.util.StringUtils;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by jiaqiZHou
 * on 2020/12/4
 */
public class LoginPresenter extends BasePresenterImpl<LoginContract.View> implements LoginContract.Presenter {

    public LoginPresenter(LoginContract.View view) {
        super(view);
    }

    @Override
    public void login(LoginRequest postBody) {
        view.showProgress(null);
        Api.getInstance().logins(postBody)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<UserInfo>>() {

                    @Override
                    protected void onSuccess(BaseRespose<UserInfo> data) {
                        if (data == null) return;
                        view.dismissProgress();
                        if(data.isSuccess()){
                            view.loginSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }

                    }

                    @Override
                    protected void onError(String msg, int errorCode) {
                        view.dismissProgress();
                        super.onError(msg, errorCode);
                    }
                });
    }


    @Override
    public void getCode(String phone,String area_code) {
        view.showProgress(null);
        Api.getInstance().getVerificationCode(phone,area_code)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<String>>() {
                    @Override
                    protected void onSuccess(BaseRespose<String> data) {
                        view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.getCodeSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        view.dismissProgress();
                        //view.getCodeFailed(errorCode,msg);
                        super.onError(msg, errorCode);
                    }
                });
    }

    /**
     * 获取验证码处理
     * @param context
     * @param etLoginName
     * @param tv_regin
     * @param tv_getcode
     */
    @Override
    public void handCode(Context context, EditText etLoginName, TextView tv_regin, TextView tv_getcode) {

        tv_getcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etLoginName.getText().toString();
                if (StringUtils.isEmpty(phone))
                {
                    CustomToast.INSTANCE.showToast(context,"手机号为空！");
                    return;
                }
                if(tv_regin.getText().equals("+86")){  //中国大陆
                    if(!CommonUtils.isCPhoneLegal(phone)){
                        CustomToast.INSTANCE.showToast(context,"请输入正确的手机号！");
                        return;
                    }
                }else if(tv_regin.getText().equals("+852")){  //中国香港

                    String regin = tv_regin.getText().toString().substring(1);
                    Log.e("regin=====",regin+"");
                    if(!CommonUtils.isHKPhoneLegal(regin+phone)){
                        CustomToast.INSTANCE.showToast(context,"请输入正确的手机号！");
                        return;
                    }
                }else if(tv_regin.getText().equals("+853")){//中国澳门
                    String regin = tv_regin.getText().toString().substring(1);
                    Log.e("regin=====",regin+"");
                    if(!CommonUtils.isMCPhoneLegal(regin+phone)){
                        CustomToast.INSTANCE.showToast(context,"请输入正确的手机号！");
                        return;
                    }
                }
                String area_code = tv_regin.getText().toString().substring(1);
                tv_getcode.setEnabled(false);
                final int count = 60;//倒计时3秒
                Observable.interval(0, 1, TimeUnit.SECONDS)
                        .take(count + 1)
                        .observeOn(AndroidSchedulers.mainThread())
                        .doOnNext(new Consumer<Long>() {
                            @Override
                            public void accept(Long aLong) throws Exception {
                                tv_getcode.setTextColor(ContextCompat.getColor(context, R.color.color_666666));
                                tv_getcode.setText("重新发送" + "（" + (count - aLong) + "s" + "）");
                            }
                        }).doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        tv_getcode.setTextColor(ContextCompat.getColor(context, R.color.color_FF798A));
                        tv_getcode.setText("获取验证码");
                        tv_getcode.setEnabled(true);
                    }
                }).subscribe();
                getCode(phone,area_code);
            }
        });
    }

    /**
     * 设置密码
     * @param map
     */
    @Override
    public void modifyPassword(Map<String, Object> map) {
        view.showProgress(null);
        Api.getInstance().modifyPassword(map)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<String>>() {
                    @Override
                    protected void onSuccess(BaseRespose<String> data) {
                        view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.setPassWordSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        view.dismissProgress();
                        //view.getCodeFailed(errorCode,msg);
                        super.onError(msg, errorCode);
                    }
                });
    }

    @Override
    public void forgetUserPassword(Map<String, Object> map) {
        view.showProgress(null);
        Api.getInstance().forgetUserPassword(map)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<String>>() {
                    @Override
                    protected void onSuccess(BaseRespose<String> data) {
                        view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.forgetUserPasswordSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        view.dismissProgress();
                        //view.getCodeFailed(errorCode,msg);
                        super.onError(msg, errorCode);
                    }
                });
    }

    @Override
    public void checkVerificationCode(Map<String, Object> map) {
        view.showProgress(null);
        Api.getInstance().checkVerificationCode(map)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<String>>() {
                    @Override
                    protected void onSuccess(BaseRespose<String> data) {
                        view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.checkVerificationCodeSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        view.dismissProgress();
                        //view.getCodeFailed(errorCode,msg);
                        super.onError(msg, errorCode);
                    }
                });
    }

}